package com.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.last.entity.*;
import com.last.mapper.RoomInfoMapper;
import com.last.service.RoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class IRoomInfoService extends ServiceImpl<RoomInfoMapper , RoomInfo> implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Override
    public PageResult<RoomInfo> search(PageDTO<RoomInfo> dataDTO) {
        IPage<RoomInfo> iPage = new Page<>(dataDTO.getPage(),dataDTO.getLimit());
        QueryWrapper<RoomInfo> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dataDTO.getData().getRoomStatus())){
           queryWrapper.eq("room_status",dataDTO.getData().getRoomStatus());
        }
        IPage<RoomInfo> page = roomInfoMapper.selectPage(iPage, queryWrapper);
        PageResult<RoomInfo> result = new PageResult<>();
        result.setData(page.getRecords());
        result.setPages(page.getPages());
        result.setTotal(page.getTotal());
        return result;
    }

    @Override
    public Result add(RoomInfo roomInfo) {
        Result result = new Result();
        try {
            // 设置审核中
            roomInfo.setHomeId(1);
            roomInfo.setRoomStatus(3);
            int i = roomInfoMapper.insert(roomInfo);
            result.setSuccess(i > 0);
            result.setMessage(i > 0 ? "添加成功" : "添加失败");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("请规范操作");
            return  result;
        }
    }

    @Override
    public Result undercarriage(Integer roomId,Integer status) {
        RoomInfo info = new RoomInfo();
        info.setRoomStatus(status);
        info.setRoomId(roomId);
        int i = roomInfoMapper.updateById(info);
        return new Result(i>0?true:false,i>0?"下架成功":"下架失败");
    }

    @Override
    public RoomInfo getRoomInfoByRoomId(Integer roomId) {
        RoomInfo roomInfoByRoomId = roomInfoMapper.getRoomInfoByRoomId(roomId);
        return roomInfoByRoomId;
    }

    @Override
    public List<City> getAllCity() {
        List<City> allCity = roomInfoMapper.getAllCity();
        return allCity;
    }

    @Override
    public List<RoomInfo> getHouseDetailInfo(Integer homeId) {
        List<RoomInfo> houseDetailInfo = roomInfoMapper.getHouseDetailInfo(homeId);
        return houseDetailInfo;
    }

    @Override
    public City findCityByHomeId(Integer homeId) {
        City cityByHomeId = roomInfoMapper.findCityByHomeId(homeId);
        return null;
    }
}
